/**
 * Created by LinJe on 2015/9/22.
 * 全局ui：登陆提示 弹窗
 */
(function( $, window ) {

    window.globalLayer = window.globalLayer || {};


    /**
     * 调用实例
     * window.globalLayer.openComment({'callBack':function() {
            console.log('登陆提示窗已关闭');
       }});
     * @param settings
     */
    window.globalLayer.openComment = function( settings ) {

        var commentArgs;

        //如果是第一次调用，加载弹窗所需html和js变量
        if ( !this.commentArgs ) {
            $('body').append('\
                <div class="layer_comment">\
                    <div class="popup-bg"></div>\
                        <div class="popup release-ugc" style="height:410px;margin-top:-205px;">\
                            <div class="content">\
                                <div class="title clearfix"><div class="text">添加讨论</div><div class="btn"><span class="close" title="关闭"></span></div></div>\
                                <div class="from comment">\
                                    <textarea name="" placeholder="你对该商品的疑问、看法、经验……"></textarea>\
                                    <div class="text-tips">剩余500字</div>\
                                <div class="button"><button type="button" class="button-default button-disabled">发布</button></div>\
                            </div>\
                        </div>\
                    </div>\
                </div>\
            ');

            commentArgs = this.commentArgs = {};
            commentArgs.$commentWrap = $('.layer_comment');
            commentArgs.$colse = commentArgs.$commentWrap.find('.title .btn');
            commentArgs.$publish = commentArgs.$commentWrap.find('.button .button-default');
            commentArgs.$textTips = commentArgs.$commentWrap.find('.text-tips');
            commentArgs.$textarea = commentArgs.$commentWrap.find('.from textarea');


            //输入框
            commentArgs.$textarea.on('keydown', function() {
                //限制字数
                $.maxTextLen({
                    'field': commentArgs.$textarea,
                    'countfield': commentArgs.$textTips,
                    'callBack': function(data) {
                        //是否激活发布按钮
                        if ( data ) {
                            commentArgs.$publish.removeClass('button-disabled');
                        } else {
                            commentArgs.$publish.addClass('button-disabled');
                        }
                    }
                });
            });
            commentArgs.$textarea.on('keyup', function() {
                //限制字数
                $.maxTextLen({
                    'field': commentArgs.$textarea,
                    'countfield': commentArgs.$textTips,
                    'callBack': function(data) {
                        //是否激活发布按钮
                        if ( data ) {
                            commentArgs.$publish.removeClass('button-disabled');
                        } else {
                            commentArgs.$publish.addClass('button-disabled');
                        }
                    }
                });
            });

            //点击取消或者X 关闭弹窗
            commentArgs.$colse.on('click', function() {
                window.globalLayer.closeComment(function(){
                    commentArgs.callBack({'content':''});
                });
            });

            //点击 发布按钮
            commentArgs.$publish.on('click', function() {
                if ( !$(this).hasClass('button-disabled') ) {
                    window.globalLayer.closeComment(function(){
                        //回调
                        commentArgs.callBack({'content':commentArgs.$textarea.val()});
                        //还原
                        commentArgs.$textarea.val('');
                        commentArgs.$textTips.removeClass('red').text('剩余500字');
                        commentArgs.$publish.addClass('button-disabled');
                    });
                }
            });
        }

        commentArgs = this.commentArgs;
        $.extend(commentArgs, settings);

        commentArgs.$commentWrap.css({'opacity':'0', 'display':'block'}).stop().animate({ 'opacity': 1 });
    };


    window.globalLayer.closeComment = function( callBack ) {

        this.commentArgs.$commentWrap.stop().animate({ 'opacity': 0 }, 500, '', function() {
            $(this).hide();
            callBack && callBack();
        });

    };

})( jQuery, window );